import {
    ADMIT_CARD,
    CLASSROOM_APPLICATION,
    CLASSROOM_MY_APPLICATION,
    COURSE_CHANGE_APPLICATION,
    COURSE_INFO,
    COURSE_STANDARD,
    COURSE_STANDARD_FILE,
    COURSEWARE,
    GRADUATION,
    MAKEUP_ARRANGEMENT,
    MY_EXAMS_ARRANGEMENT,
    MY_EXAMS_REMINDER,
    RESULTS,
    RETAKE,
    RETAKE_ARRANGEMENT,
    SCHEDULE_INFO,
    TALENT_PLAN,
    TEACHER_INFO,
    TEACHING_PLAN,
    UPGRADE,
    WARNING_REMINDER
} from "../../api/api";
import {getLocalStorage} from "@/util/storage.util";
import {getDataByGet, getDataByPost} from "@/util/getdata.util";
import {ElMessage} from "element-plus";
import axios from "axios";

// 获取学生培养方案
export function getStudentTalentPlan(callback: Function) {
    let UserInfo = JSON.parse(<string>getLocalStorage("UserInfo"))
    let studentNumber: string = UserInfo.userInfo.referenceId
    return `http://localhost:8080${TALENT_PLAN}?student-number=${studentNumber}`
}

// 获取学生课程信息
export function getStudentCourseInfo(callback: Function) {
    let UserInfo = JSON.parse(<string>getLocalStorage("UserInfo"))
    let studentNumber: string = UserInfo.userInfo.referenceId
    getDataByGet(`http://localhost:8080${COURSE_INFO}?student-number=${studentNumber}`, null)
        .then((res: any) => {
            if (res.code === 200) {
                callback(res.datas)
            }
        })
        .catch(err => {
            ElMessage({
                message: "获取课程信息失败",
                type: "error",
                duration: 2000
            });
        })
}

// 获取学生教学资料课程标准
export function getStudentCourseStandard(department: any, courseId: any, courseName: any, callback: Function) {
    let UserInfo = JSON.parse(<string>getLocalStorage("UserInfo"))
    let studentNumber: string = UserInfo.userInfo.referenceId
    getDataByGet(`${COURSE_STANDARD}?student-number=${studentNumber}&department=${department}&courseId=${courseId}&courseName=${courseName}`, null)
        .then((res: any) => {
            callback(res.datas)
        })
        .catch((err: any) => {
            ElMessage({
                message: "获取课程标准失败",
                type: "error",
                duration: 2000
            });
        })
}

// 课程标准查询
export function searchStudentCourseStandard(courseId: any) {
    return `http://localhost:8080${COURSE_STANDARD_FILE}?courseId=${courseId}`
}

// 获取学生教学资料教学计划
export function getStudentTeachingPlan(callback: Function) {
    let UserInfo = JSON.parse(<string>getLocalStorage("UserInfo"))
    let studentNumber: string = UserInfo.userInfo.referenceId
    return `http://localhost:8080${TEACHING_PLAN}?student-number=${studentNumber}`
}

// 获取学生课件页面基础数据
export function getStudentCourseWareView(callback: Function) {
    let UserInfo = JSON.parse(<string>getLocalStorage("UserInfo"))
    let studentNumber: string = UserInfo.userInfo.referenceId
    getDataByGet(`${COURSEWARE}?student-number=${studentNumber}`, null)
        .then((res: any) => {
            if (res.code === 200) {
                callback(res.datas)
            }
        })
        .catch((err: any) => {
            ElMessage({
                message: "服务器出错了*-*",
                type: "error",
                duration: 2000
            });
        })
}

// 获取学生教学资料课件
export function getStudentCourseWare(courseId: any, dir: any, fileName: any, callback: Function) {
    const url = `http://localhost:8080/student/teaching-data/materials/get-courseware?dir=${dir}&fileName=${fileName}&courseId=${courseId}`
    axios.get(url, {
        responseType: 'blob' // 以二进制流的形式接收文件数据
    }).then((response: any) => {
        const blob = new Blob([response.data], {type: response.headers['content-type']});
        const downloadUrl = window.URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = downloadUrl;
        a.download = fileName;
        document.body.appendChild(a);
        a.click();
        window.URL.revokeObjectURL(downloadUrl);
        document.body.removeChild(a);

        if (callback) callback();
    }).catch((error: any) => {
        console.error("文件下载失败:", error);
        if (callback) callback(error);
    });
}

// 获取学生教师信息
export function getStudentTeacherInfo(callback: Function) {
    let UserInfo = JSON.parse(<string>getLocalStorage("UserInfo"))
    let studentNumber: string = UserInfo.userInfo.referenceId
    getDataByGet(`http://localhost:8080${TEACHER_INFO}?student-number=${studentNumber}`, null)
        .then((res: any) => {
            if (res.code === 200) {
                callback(res.datas)
            }
        })
        .catch(err => {
            ElMessage({
                message: "获取教师信息失败",
                type: "error",
                duration: 2000
            });
        })
}

// 获取学生课表查询
export function getStudentScheduleInfo(callback: Function) {
    let UserInfo = JSON.parse(<string>getLocalStorage("UserInfo"))
    let studentNumber: string = UserInfo.userInfo.referenceId
    getDataByGet(`http://localhost:8080${SCHEDULE_INFO}?student-number=${studentNumber}`, null)
        .then((res: any) => {
            if (res.code === 200) {
                callback(res.datas)
            }
        })
        .catch(err => {
            ElMessage({
                message: "获取课表失败",
                type: "error",
                duration: 2000
            });
        })
}

// 获取考务管理——我的考试——考试安排查询
export function getStudentMyExamsArrangement(callback: Function) {
    let UserInfo = JSON.parse(<string>getLocalStorage("UserInfo"))
    let studentNumber: string = UserInfo.userInfo.referenceId
    getDataByGet(`http://localhost:8080${MY_EXAMS_ARRANGEMENT}?student-number=${studentNumber}`, null)
        .then((res: any) => {
            if (res.code === 200) {
                callback(res.datas)
            }
        })
        .catch(err => {
            ElMessage({
                message: "服务器出错了*-*",
                type: "error",
                duration: 2000
            });
        })
}

// 获取考务管理——我的考试——考试提醒
export function getStudentMyExamsReminder(callback: Function) {
    let UserInfo = JSON.parse(<string>getLocalStorage("UserInfo"))
    let studentNumber: string = UserInfo.userInfo.referenceId
    getDataByGet(`http://localhost:8080${MY_EXAMS_REMINDER}?student-number=${studentNumber}`, null)
        .then((res: any) => {
            if (res.code === 200) {
                callback(res.datas)
            }
        })
        .catch(err => {
            ElMessage({
                message: "服务器出错了*-*",
                type: "error",
                duration: 2000
            });
        })
}

// 获取准考证打印
export function getStudentAdmitCard(callback: Function) {
    let UserInfo = JSON.parse(<string>getLocalStorage("UserInfo"))
    let studentNumber: string = UserInfo.userInfo.referenceId
    getDataByGet(`http://localhost:8080${ADMIT_CARD}?student-number=${studentNumber}`, null)
        .then((res: any) => {
            if (res.code === 200) {
                callback(res.datas)
            }
        })
        .catch(err => {
            ElMessage({
                message: "服务器出错了*-*",
                type: "error",
                duration: 2000
            });
        })
}

// 获取学生考试成绩查询
export function getStudentResults(callback: Function) {
    let UserInfo = JSON.parse(<string>getLocalStorage("UserInfo"))
    let studentNumber: string = UserInfo.userInfo.referenceId
    getDataByGet(`http://localhost:8080${RESULTS}?student-number=${studentNumber}`, null)
        .then((res: any) => {
            if (res.code === 200) {
                callback(res.datas)
            }
        })
        .catch(err => {
            ElMessage({
                message: "获取考试成绩失败",
                type: "error",
                duration: 2000
            });
        })
}

// 获取学生补考安排
export function getStudentMakeupArrangement(callback: Function) {
    let UserInfo = JSON.parse(<string>getLocalStorage("UserInfo"))
    let studentNumber: string = UserInfo.userInfo.referenceId
    getDataByGet(`http://localhost:8080${MAKEUP_ARRANGEMENT}?student-number=${studentNumber}`, null)
        .then((res: any) => {
            if (res.code === 200) {
                callback(res.datas)
            }
        })
        .catch(err => {
            ElMessage({
                message: "服务器出错了*-*",
                type: "error",
                duration: 2000
            });
        })
}

// 获取学生重修选课
export function getStudentRetake(callback: Function) {
    let UserInfo = JSON.parse(<string>getLocalStorage("UserInfo"))
    let studentNumber: string = UserInfo.userInfo.referenceId
    getDataByGet(`http://localhost:8080${RETAKE}?student-number=${studentNumber}`, null)
        .then((res: any) => {
            if (res.code === 200) {
                callback(res.datas)
            }
        })
        .catch(err => {
            ElMessage({
                message: "服务器出错了*-*",
                type: "error",
                duration: 2000
            });
        })
}

// 获取学生重修考试安排
export function getStudentRetakeArrangement(callback: Function) {
    let UserInfo = JSON.parse(<string>getLocalStorage("UserInfo"))
    let studentNumber: string = UserInfo.userInfo.referenceId
    getDataByGet(`http://localhost:8080${RETAKE_ARRANGEMENT}?student-number=${studentNumber}`, null)
        .then((res: any) => {
            if (res.code === 200) {
                callback(res.datas)
            }
        })
        .catch(err => {
            ElMessage({
                message: "服务器出错了*-*",
                type: "error",
                duration: 2000
            });
        })
}

// 获取学生学业预警提醒
export function getStudentWarningReminder(callback: Function) {
    let UserInfo = JSON.parse(<string>getLocalStorage("UserInfo"))
    let studentNumber: string = UserInfo.userInfo.referenceId
    getDataByGet(`http://localhost:8080${WARNING_REMINDER}?student-number=${studentNumber}`, null)
        .then((res: any) => {
            if (res.code === 200) {
                callback(res.datas)
            }
        })
        .catch(err => {
            ElMessage({
                message: "服务器出错了*-*",
                type: "error",
                duration: 2000
            });
        })
}

// 获取学生教室申请
export function getStudentClassroomApplication(campusId: any, callback: Function) {
    let UserInfo = JSON.parse(<string>getLocalStorage("UserInfo"))
    let studentNumber: string = UserInfo.userInfo.referenceId
    getDataByGet(`http://localhost:8080${CLASSROOM_APPLICATION}?student-number=${studentNumber}&campusId=${campusId}`, null)
        .then((res: any) => {
            if (res.code === 200) {
                callback(res.datas)
            }
        })
        .catch(err => {
            ElMessage({
                message: "服务器出错了*-*",
                type: "error",
                duration: 2000
            });
        })
}

// 获取学生我的申请
export function getStudentClassroomMyApplication(callback: Function) {
    let UserInfo = JSON.parse(<string>getLocalStorage("UserInfo"))
    let studentNumber: string = UserInfo.userInfo.referenceId
    getDataByGet(`http://localhost:8080${CLASSROOM_MY_APPLICATION}?student-number=${studentNumber}`, null)
        .then((res: any) => {
            if (res.code === 200) {
                callback(res.datas)
            }
        })
        .catch(err => {
            ElMessage({
                message: "服务器出错了*-*",
                type: "error",
                duration: 2000
            });
        })
}

export function postStudentClassroomApplication(form: any, callback: Function) {
    getDataByPost(`http://localhost:8080${CLASSROOM_APPLICATION}`, form, null)
        .then((res: any) => {
            if (res.code === 200) {
                ElMessage({
                    message: "提交成功",
                    type: "success",
                    duration: 2000
                })
                callback()
            }
        })
        .catch((err) => {
            ElMessage({
                message: "服务器出错了*-*",
                type: "error",
                duration: 2000
            })
        })
}

// 获取学生调课申请
export function getStudentCourseChangeApplication(callback: Function) {
    let UserInfo = JSON.parse(<string>getLocalStorage("UserInfo"))
    let studentNumber: string = UserInfo.userInfo.referenceId
    getDataByGet(`http://localhost:8080${COURSE_CHANGE_APPLICATION}?student-number=${studentNumber}`, null)
        .then((res: any) => {
            if (res.code === 200) {
                callback(res.datas)
            }
        })
        .catch(err => {
            ElMessage({
                message: "服务器出错了*-*",
                type: "error",
                duration: 2000
            });
        })
}

// 提交学生调课申请
export function postStudentCourseChangeApplication(form: any, callback: Function) {
    let UserInfo = JSON.parse(<string>getLocalStorage("UserInfo"))
    let studentNumber: string = UserInfo.userInfo.referenceId
    getDataByPost(`http://localhost:8080${COURSE_CHANGE_APPLICATION}?student-number=${studentNumber}`, form, null)
        .then((res: any) => {
            if (res.code === 200) {
                ElMessage({
                    message: "提交成功",
                    type: "success",
                    duration: 2000
                });
                callback()
            }
        })
        .catch(err => {
            ElMessage({
                message: "提交失败",
                type: "error",
                duration: 2000
            });
        })
}

// 获取学生毕业审核
export function getStudentGraduation(callback: Function) {
    let UserInfo = JSON.parse(<string>getLocalStorage("UserInfo"))
    let studentNumber: string = UserInfo.userInfo.referenceId
    getDataByGet(`http://localhost:8080${GRADUATION}?student-number=${studentNumber}`, null)
        .then((res: any) => {
            if (res.code === 200) {
                callback(res.datas)
            }
        })
        .catch(err => {
            ElMessage({
                message: "服务器出错了*-*",
                type: "error",
                duration: 2000
            });
        })
}

// 获取学生专升本报名
export function getStudentUpgrade(callback: Function) {
    let UserInfo = JSON.parse(<string>getLocalStorage("UserInfo"))
    let studentNumber: string = UserInfo.userInfo.referenceId
    getDataByGet(`http://localhost:8080${UPGRADE}?student-number=${studentNumber}`, null)
        .then((res: any) => {
            if (res.code === 200) {
                callback(res.datas)
            }
        })
        .catch(err => {
            ElMessage({
                message: "服务器出错了*-*",
                type: "error",
                duration: 2000
            });
        })
}